package wang.z.component;

import wang.z.JFrame.loginFrm;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.PathUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * @author like
 * @date 2022/12/13 14:58
 * @Description TODO
 */
public class BookTypeAdd {
    private JTextField textField_1;//类别名称
    private JTextArea textField_2;//类别说明
    private JPanel jPanel = new JPanel();
    private JLabel label_1;///类别名称
    private JLabel label_2;///类别说明
    private JButton addBtn;//添加
    private JButton tBnt;//重置
    public BookTypeAdd(JFrame jf) throws IOException {

        textField_1 = new JTextField();
        textField_1.setColumns(18);
        JLabel label_1 = new JLabel("类别名称：");
        Box uBox = Box.createHorizontalBox();
        label_1.setFont(new Font("幼圆",Font.BOLD, 18));
        uBox.add(label_1);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(textField_1);


        Box pBox = Box.createHorizontalBox();
        JLabel label_2 = new JLabel("类别说明：");
        label_2.setFont(new Font("幼圆", Font.BOLD, 18));
        textField_2 = new JTextArea(15,18);
        pBox.add(label_2);
        pBox.add(Box.createHorizontalStrut(19));
        pBox.add(textField_2);

        Box lBox = Box.createHorizontalBox();
        addBtn = new JButton("添加");
        addBtn.setBounds(130,230,100,27);
        addBtn.setFont(new Font("幼圆", Font.BOLD, 14));
        lBox.add(addBtn);
        lBox.add(Box.createHorizontalStrut(50));
        tBnt = new JButton("重置");
        tBnt.setBounds(300,230,100,27);
        tBnt.setFont(new Font("幼圆", Font.BOLD, 14));
        lBox.add(tBnt);

        Box vBox = Box.createVerticalBox();
        BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("library.jpg"))));
        bgPanel.setBounds(10,10,600,500);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(lBox);
        bgPanel.add(vBox);
        jf.add(bgPanel);


        addBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Map<String ,String> params = new HashMap<>();;
                String typeName = textField_1.getText().trim();
                String remark = textField_2.getText().trim();

                params.put("typeName",typeName);
                params.put("remark",remark);

                PostUtils.postWithParams("http://localhost:8080/book_type/AddType", params,new SuccessListener() {
                            //请求发送成功，服务器正常响应，会执行这里
                            @Override
                            public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                                ResultInfo info = JsonUtils.parseResult(result);
                                //判断info中的flag标记
                                if (info.isFlag()) {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());

                                } else {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());
                                    if(info.getMessage().equals("用户尚未登录")){
                                        new loginFrm().init();
                                        jf.dispose();
                                    }
                                }
                            }
                        }
                        , new FailListener() {
                            //当该请求发送失败后会执行这个方法
                            @Override
                            public void fail() {
                                JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                            }
                        }
                );
            }
        });
        tBnt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField_1.setText("");
                textField_2.setText("");
            }
        });


    }
}
